package com.kaiwukj.android.ufamily.mvp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.kaiwukj.android.mcas.utils.UToast;
import com.kaiwukj.android.ufamily.R;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter {
    public static final int IMG = 1, ADD = 2;
    private List<String> imgUrls;
    private Context context;
    private int maxCount = -1;

    public ImageAdapter(Context context, List<String> imgUrls) {
        this.context = context;
        this.imgUrls = imgUrls;
    }

    public ImageAdapter() {
        imgUrls = new ArrayList<>();
    }

    public void setData(List<String> imgUrls) {
        this.imgUrls = imgUrls;
        notifyDataSetChanged();
    }

    public void setMaxCount(int count) {
        maxCount = count;
    }

    public void addData(List<String> data) {
        if (imgUrls == null) {
            imgUrls = new ArrayList<>();
        }
        imgUrls.addAll(data);
        notifyDataSetChanged();
        //notifyItemRangeChanged();
    }

    public List<String> getData() {
        return imgUrls;
    }

    public void remove(int position) {
        try {
            imgUrls.remove(position);
            notifyItemRemoved(position);
        } catch (IndexOutOfBoundsException e) {
            UToast.showShort(context, "数据异常!!!");
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        if (viewType == IMG) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_upload_img, viewGroup, false);
            return new ImgHolder(view);
        } else if (viewType == ADD) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_upload_img_add, viewGroup, false);
            return new AddImgHolder(view);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_upload_img, viewGroup, false);
        return new ImgHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
        if (getItemViewType(position) == IMG) {
            ImgHolder imgHolder = (ImgHolder) viewHolder;
            Glide.with(context).load(imgUrls.get(viewHolder.getLayoutPosition())).into(imgHolder.iv);

            imgHolder.ivDel.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onDelClick(viewHolder.getLayoutPosition());
                }
            });
            imgHolder.iv.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onImgClick(imgUrls, viewHolder.getLayoutPosition());
                }
            });


        } else if (getItemViewType(viewHolder.getLayoutPosition()) == ADD) {
            AddImgHolder addImgHolder = (AddImgHolder) viewHolder;
            addImgHolder.itemView.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onAddClick();
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        if (maxCount > 0 && imgUrls.size() == maxCount) {
            return imgUrls.size();
        }
        return imgUrls.size() + 1;
    }

    public int getDataCount() {
        return imgUrls.size();
    }

    @Override
    public int getItemViewType(int position) {
     /*   if (imgUrls.size() == maxCount) {
            return IMG;
        }*/
        return position == imgUrls.size() ? ADD : IMG;
    }

    public interface onItemClickListener {
        void onAddClick();

        void onDelClick(int position);

        void onImgClick(List<String> imgUrls, int position);
    }

    onItemClickListener clickListener;

    public void setOnItemClickListener(onItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    static class AddImgHolder extends RecyclerView.ViewHolder {
        //RelativeLayout rl;

        public AddImgHolder(@NonNull View itemView) {
            super(itemView);
            // rl = itemView.findViewById(R.id.item_community_publish_add);
        }
    }

    static class ImgHolder extends RecyclerView.ViewHolder {
        ImageView iv, ivDel;

        public ImgHolder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv_image);
            ivDel = itemView.findViewById(R.id.iv_delete_img);
        }
    }
}
